<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="handleCloseDialog" @open="handleOpenDialog">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList" v-loading="loading">
      <!--
    收集到数组中的值是由label指定的!! 他也可以指定文字描述
    但是后台收集的都是id, 所以label需要指定成id, 但是文字描述用户就看不懂了,
    这里可以换另一种形式指定文字描述, 直接写在双标签内部!!
  -->
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"

        :label="item.id"
      >
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="clickAssignRoles">确定</el-button>
      <el-button size="small">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetRoleList } from '@/api/setting'
import { reqGetUserId } from '@/api/user'
import { reqAssignRoles } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      checkList: [],
      roleList: [],
      loading: false
    }
  },
  methods: {
    handleCloseDialog() {
      this.$emit('update:showRoleDialog', false)
      this.checkList = []
    },
    handleOpenDialog() {
      this.loading = true
      Promise.all([
        this.getRoleList(),
        this.getEmployeeDetail()
      ]).then(() => {
        this.loading = false
      })
      this.getRoleList()
      this.getEmployeeDetail()
    },
    async getRoleList() {
      const { data: { rows }} = await reqGetRoleList(1, 999999)
      // console.log(res)
      this.roleList = rows
      console.log(rows)
    },

    async getEmployeeDetail() {
      const { data: { roleIds }} = await reqGetUserId(this.userId)
      // console.log(res)
      this.checkList = roleIds
    },
    async clickAssignRoles() {
      await reqAssignRoles(this.userId, this.checkList)

      this.$message.success('分配角色成功')
      this.handleCloseDialog()
    }

  }
}
</script>
